<?php

namespace addons\aliyunsms\library;

use AlibabaCloud\Client\AlibabaCloud;

class Aliyunsms
{
    private $_params = [];
    protected $error = '';
    protected $config = [];
    protected static $instance = null;
    protected $statusStr = array(
        "0" => "短信发送成功",
        "-1" => "参数不全",
        "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
        "30" => "密码错误",
        "40" => "账号不存在",
        "41" => "余额不足",
        "42" => "帐户已过期",
        "43" => "IP地址限制",
        "50" => "内容含有敏感词"
    );


    /**
     * Aliyunsms constructor.
     * @param array $options
     * @throws \AlibabaCloud\Client\Exception\ClientException
     */
    public function __construct($options = [])
    {
        if ($config = get_addon_config('aliyunsms')) {
            $this->config = array_merge($this->config, $config);
        }
        $this->config = array_merge($this->config, is_array($options) ? $options : []);
        AlibabaCloud::accessKeyClient($this->config['accessKeyId'], $this->config['accessSecret'])
            ->regionId($this->config['regionId'])
            ->asDefaultClient();
    }

    /**
     * 单例
     * @param array $options 参数
     * @return Aliyunsms
     */
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($options);
        }
        return self::$instance;
    }

    /**
     * 立即发送短信
     *
     * @return boolean
     * @throws \AlibabaCloud\Client\Exception\ClientException
     * @throws \AlibabaCloud\Client\Exception\ServerException
     */
    public function send()
    {
        $this->error = '';
        $params = $this->_params();


        $params_post = [
            'code' => $params['msg']
        ];


        $result = AlibabaCloud::rpc()
            ->product('Dysmsapi')
            // ->scheme('https') // https | http
            ->version('2017-05-25')
            ->action('SendSms')
            ->method('POST')
            ->host('dysmsapi.aliyuncs.com')
            ->options([
                'query' => [
                    'RegionId' => $this->config['regionId'],
                    'PhoneNumbers' => $this->_params['mobile'],
                    'SignName' => $this->config['signName'],
                    'TemplateCode' => $this->config['templateCode'],
                    'TemplateParam' => json_encode($params_post),
                ],
            ])
            ->request();

        $result = $result->toArray();

        if ($result['Code'] == "OK") {
            return true;
        } else {
            $this->error = $result['Message'];
        }
        return false;
    }

    private function _params()
    {
        return $this->_params;
    }

    /**
     * 获取错误信息
     * @return string
     */
    public function getError()
    {
        return $this->error;
    }

    /**
     * 接收手机
     * @param string $mobile 手机号码
     * @return Aliyunsms
     */
    public function mobile($mobile = '')
    {
        $this->_params['mobile'] = $mobile;
        return $this;
    }

    /**
     * 短信内容
     * @param string $msg 短信内容
     * @return Aliyunsms
     */
    public function msg($msg = '')
    {
        $this->_params['msg'] = $msg;
        return $this;
    }
}